﻿window.requestAnimFrame = (function () {
    return window.requestAnimationFrame ||
              window.webkitRequestAnimationFrame ||
              window.mozRequestAnimationFrame ||
              window.oRequestAnimationFrame ||
              window.msRequestAnimationFrame ||
              function (/* function */callback, /* DOMElement */element) {
                  window.setTimeout(callback, 1000 / 60);
              };
          })();

function MotorJuego() {
    this.entities = [];    
    this.ctx = null;
    this.click = null;
    this.mouse = null;
    this.timer = new Reloj();
    this.surfaceWidth = null;
    this.surfaceHeight = null;
    this.halfSurfaceWidth = null;
    this.halfSurfaceHeight = null;
}

MotorJuego.prototype.init = function (ctx) {
    this.ctx = ctx;
    this.surfaceWidth = this.ctx.canvas.width;
    this.surfaceHeight = this.ctx.canvas.height;
    this.halfSurfaceWidth = this.surfaceWidth / 2;
    this.halfSurfaceHeight = this.surfaceHeight / 2;
    this.startInput();
    console.log('juego initialized');
}

MotorJuego.prototype.start = function () {
    console.log("starting juego");
    var that = this;
    (function gameLoop() {
        that.loop();
        requestAnimFrame(gameLoop, that.ctx.canvas);
    })();
}

MotorJuego.prototype.startInput = function () {
    console.log('Starting input');

    var getXandY = function (e) {
        var x = parseInt(e.clientX - that.ctx.canvas.getBoundingClientRect().left);// - (that.ctx.canvas.width / 2);
        var y = parseInt(e.clientY - that.ctx.canvas.getBoundingClientRect().top);// - (that.ctx.canvas.height / 2);

        var row = parseInt(y * that.mapHeight / that.ctx.canvas.height);
        var col = parseInt(x * that.mapWidth / that.ctx.canvas.width);

        return { x: x, y: y, row: row, col: col };
    }    

    var that = this;

    this.ctx.canvas.addEventListener("click", function (e) {
        that.click = getXandY(e);        
    }, false);

    this.ctx.canvas.addEventListener("mousemove", function (e) {
        that.mouse = getXandY(e);
    }, false);

    console.log('Input started');
}

MotorJuego.prototype.agregarObjeto = function (objeto, alPrincipio) {
    //si es mina, agregarla al principio
    if(alPrincipio)
        this.entities.unshift(objeto);
    else
        this.entities.push(objeto);
}

MotorJuego.prototype.update = function () {
    var entitiesCount = this.entities.length;

    for (var i = 0; i < entitiesCount; i++) {
        var objeto = this.entities[i];
        if (!objeto.removeFromWorld) {
            objeto.update();
        }
    }

    for (var i = this.entities.length - 1; i >= 0; --i) {
        if (this.entities[i].removeFromWorld) {
            this.entities.splice(i, 1);
        }
    }
}

MotorJuego.prototype.draw = function (drawCallback) {    
    this.ctx.clearRect(0, 0, this.ctx.canvas.width, this.ctx.canvas.height);

    this.drawMap();
    //this.drawGrid("black");

    for (var i = 0; i < this.entities.length; i++) {
        this.entities[i].draw(this.ctx);
    }
    if (drawCallback) {
        drawCallback(this);
    }   
}

MotorJuego.prototype.loop = function () {
    this.clockTick = this.timer.tick();
    this.update();
    this.draw();
    this.click = null;
}